



import json
import time
import re

import requests
import random


header0={ "User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36" }
# header0={ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0"}
header={'Referer': 'https://www.bilibili.com'}


for k in header0.keys():
    header[k]=header0[k]

cookies=None

def getCookie():
    page=requests.get("https://www.bilibili.com/",headers=header0)
    cook=page.cookies
    return cook

def fetch(url):
    global cookies
    if not cookies:
        cookies=getCookie()
    if not cookies:
        print("[E] error : can't get cookies ")
        return
    return requests.get(url,cookies=cookies,headers=header)


# 免费
# https://www.bilibili.com/documentary/index/
# documentary == 纪录片
# 92 pages , 有些是 限免的
uu="https://api.bilibili.com/pgc/season/index/result?style_id=-1&producer_id=-1&release_date=-1&season_status=1&order=2&st=3&sort=0&page={}&season_type=3&pagesize=20&type=1"
pages=92
for i in range(pages):
    print("[i] page=",i+1)
    page_data=[]
    try:
        res=fetch(uu.format(i+1))
        jj=json.loads(res.text)
        for jo in jj["data"]["list"]:
            line1_pre=jo.get("badge","")
            if line1_pre:
                line1_pre=line1_pre+":"
            line1="#"+line1_pre+jo.get("title","")
            line2=jo.get("link","")
            page_data.append(line1)
            page_data.append(line2)
            print(line1)
            print(line2)
    except Exception as e:
        print(e)

    with open("bangumi.10.free_bili_ji_lu_pian.%04d.txt"%(i+1),"w") as f:
        for line in page_data:
            _=f.write(line)
            _=f.write("\n")
    print("[i] DONE page=",i+1)
    time.sleep(8+random.random()*4)




